<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

class trade_cancel extends PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        // $this->model = app::get('b2c')->model('goods_type');
    }

   public function test()
    {
        //默认24小时未支付的订单需要取消订单
        $time_out = 1440;

        //计算时间戳：这个时间之前的订单需要取消
        $time_cur = time() - $time_out*60;
        $order_mdl = app::get('b2c')->model('orders');

        //搜索条件
        $filter['createtime|sthan'] = $time_cur;
        $filter['pay_status'] = 0;
        $filter['status'] = 'active';
        $orders = $order_mdl->getList("order_id,pay_status",$filter);
        $this->cancel_orders($orders);
    }

    function cancel_orders($orders)
    {
        $obj_checkorder = kernel::service('b2c_order_apps', array('content_path'=>'b2c_order_checkorder'));
        $mdl_order_cancel_reason = app::get('b2c')->model('order_cancel_reason');
        $b2c_order_cancel = kernel::single("b2c_order_cancel");
        foreach($orders as $key=>$val){
            $oid = $val['order_id'];
            $o_reason = "未支付订单，系统自动取消";

            if($obj_checkorder->check_order_cancel($oid))
            {
                $sdf['order_id'] = $oid;
                $sdf['op_id'] = 1;
                $sdf['opname'] = 'admin';
                $sdf['account_type'] = 'shopadmin';

                $order_cancel_reason = array(
                    'order_id' => $oid,
                    'reason_type' => 7,
                    'reason_desc' => $o_reason,
                    'cancel_time' => time(),
                );

                if ($b2c_order_cancel->generate($sdf,$null, $message)){
                    $result = $mdl_order_cancel_reason->save($order_cancel_reason);
                    if($order_object = kernel::service('b2c_order_rpc_async')){
                        $order_object->modifyActive($sdf['order_id']);
                    }
                }
            }
        }
    }
}
